<?php
/**
 * $Id: vote.php 33 2010-10-15 05:46:59Z varun $
 * 
 * This file can not be redistributed to anyone without the permission of Giga Promoters
 * Copyright 2005 - 2008, Giga Promoters
 * 						  608 Aggarwal Cyber Plaza
 *						  Netaji Subhash Place, Delhi - 110034
 *						  www.gigapromoters.com
 * 
 *
 * @version 			$Rev: 33 $
 * @lastModifiedBy		$LastChangedBy: varun $
 * @lastChanged			$Date: 2010-10-15 11:16:59 +0530 (Fri, 15 Oct 2010) $
 * @copyright 			Giga Promoters (www.gigapromoters.com)
 * 
 */

class VoteHelper extends AppHelper {

	var $helpers = array('Javascript', 'Html');
	var $__model;
	
	// output javascript and css as well
	function beforeRender()
	{
		$view =& ClassRegistry::getObject('view');
		$this->Javascript->link('/votes/js/vote.js',false);
		//$this->Html->css('/votes/css/vote.css',null,array(),false);
	}

	// digg style voting
	function diggStyle($item_id=null, $type=null)
	{
		$settings = array();
		$settings['type'] = $type;
		$settings['item_id'] = $item_id;

		// find no. of + votes
		$settings = am($settings,$this->getVotesCount($item_id,$type));
		$view = ClassRegistry::getObject('View');
		return $view->element('vote', array('settings'=> $settings,'plugin'=>'votes' ));
	}
	function yahooStyle($item_id=null, $type=null)
	{
		$settings = array();
		$settings['type'] = $type;
		$settings['item_id'] = $item_id;
		$settings = am($settings,$this->getVotesCount($item_id,$type));
		$view =& ClassRegistry::getObject('view');
		return $view->element('yahooStyle', array('settings'=> $settings,'plugin'=>'votes'  ));
	}
	
	function simple($item_id, $type)
	{
		$settings = array();
		$settings['type'] = $type;
		$settings['item_id'] = $item_id;	
		$settings = am($settings,$this->getVotesCount($item_id,$type));
		
		$view =& ClassRegistry::getObject('view');
		return $view->element('vote_simple', array('settings'=> $settings,'plugin'=>'votes'  ));
	}
	
	protected function getVotesCount($itemId,$type)
	{
		$this->__model = $this->getModel('Votes','Vote');
		$votes = array();
		$votes['votesUp'] = $this->__model->getVotesCount($type,$itemId,Vote::TYPE_UP);
		$votes['votesDown'] = $this->__model->getVotesCount($type,$itemId,Vote::TYPE_DOWN);
		$votes['totalVotes'] = $votes['votesUp'] + $votes['votesDown'];
		$votes['score'] = $votes['votesUp'] - $votes['votesDown'];
		return $votes;
	}
}
?>